package com.example.sls.dao.logistics;

import com.example.sls.pojo.logistics.Announcements;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 公告管理表(Announcements)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-24 08:24:24
 */
public interface AnnouncementsDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from announcements where id=#{id}")
  Announcements queryById(Integer id);
  /**
   * 查询指定行数据
   *
   * @param announcements 查询条件
   * @param pageSize      分页对象
   * @param offset        偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from announcements where 1=1" +
          " and (id=#{announcements.id} or (isnull(#{announcements.id}) or #{announcements.id}=0))" +
          " and (title=#{announcements.title} or (isnull(#{announcements.title}) or #{announcements.title}=''))" +
          " and (content=#{announcements.content} or (isnull(#{announcements.content}) or #{announcements.content}=''))" +
          " and (category=#{announcements.category} or (isnull(#{announcements.category}) or #{announcements.category}=''))" +
          " and (created_at=#{announcements.createdAt} or (isnull(#{announcements.createdAt}) or #{announcements.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<Announcements> queryAllByLimit(Announcements announcements, Integer pageSize, Integer offset);
  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from announcements")
  long count();
  /**
   * 新增数据
   *
   * @param announcements 实例对象
   * @return 影响行数
   */
  @Insert("insert into announcements (id,title,content,category,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(!isnull(#{title}),#{title},null)," +
          "if(!isnull(#{content}),#{content},null)," +
          "if(!isnull(#{category}),#{category},null)," +
          "if(!isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(Announcements announcements);
  /**
   * 通过id修改数据
   * id 主键
   *
   * @param announcements 实例对象
   * @return 影响行数
   */
  @Update("update announcements set " +
          "title=if(isnull(#{title}),title,#{title})," +
          "content=if(isnull(#{content}),content,#{content})," +
          "category=if(isnull(#{category}),category,#{category})," +
          "created_at=if(isnull(#{createdAt}),created_at,#{createdAt})" +
          " where id=#{id}")
  int update(Announcements announcements);
  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from announcements where id=#{id}")
  int deleteById(Integer id);
}

